<?php
/*************************************
Class Category
Cac phuong thuc:
-Create: tao categories
-GetChildren: lay cac category con
-Update: cap nhat thong tin cua 1 category
-Delete: xoa ~
-GetInfo: lay thong tin 1 category
*************************************/
class Category
{
    public $data;	//Thông tin 1 category
    public $path;	//Đường dẫn từ category gốc tới category hiện tại
    public $sub;	//Các category con của category hiện tại
    public $folder;	//Đường dẫn tới thư mục lưu ảnh của 1 cateogry
	public $items=array();	//Các item trong category hiện tại
	public $all_items;
	public $max_item=30;
	public $page_num;
	
    public function __construct($id)
    {
        global $fcache;
		//Nếu đã tồn tại trong cache thì load từ cache
        if(isset($fcache[$id]))
        {
            $this->data = $fcache[$id]->data;
            $this->path = $fcache[$id]->path;
            $this->sub = $fcache[$id]->sub;
            $this->folder = $fcache[$id]->folder;
			$this->items=$fcache[$id]->items;
            return;
        }
		
		//Nếu chưa thì thực hiện query
        $this->data['id']=$id;
        $this->GetInfo();
        $this->path=array();
        $this->folder=$this->data['folder']!=""?$this->data['folder'] . "/":"";
        if($this->data['id']!=$this->data['pid'])
        {
            $cat=new Category($this->data['pid']);
            $this->path=$cat->path;
            array_push($this->path,$cat->data);
            $this->folder=strlen($cat->data['folder'])!=0?$cat->folder . $this->folder:$this->folder;
        }
        
        $this->sub=Category::GetChildren($id);
		$this->items=$this->GetItems();
        $fcache[$id] = $this;
    }
    private function GetInfo()
    {
        global $db;
        
		$query = "SELECT * FROM categories WHERE " . $db->sql_in_set("id",array($this->data['id']));
		$result = $db->sql_query($query);
		$row=$db->sql_fetchrow($result);
        $this->data=$row;
    }
	public static function Create($name, $tag, $folder, $pid)
	{
		global $db,$root_path;
		$query = array (
			'catname' 			=> $name,
			'pid'			=> $pid,
			'tag'	=> $tag,
			'folder'		=>	$folder,
		);
		$cat=new Category($pid);
		@mkdir($root_path . 'images/'.$cat->folder . $folder);
		$query_str = "INSERT INTO categories " . $db->sql_build_array('INSERT',$query);
		return $db->sql_query($query_str);
	}
	private static function GetChildren($id)
	{
		global $db;
		$query= array(
			'SELECT'	=> 'id,catname,tag,folder',
			'FROM'		=> array(
				'categories'	=> "c"
			),
			'WHERE'		=> $db->sql_in_set('pid',array($id)),
		);
		$result = $db->sql_query($db->sql_build_query("SELECT",$query));
		$rowset=array();
		$i=0;
		while($row=$db->sql_fetchrow($result))
		{
            if($row['id']!=0)
                $rowset[$i++]=$row;
		}
		return $rowset;
	}
	public function Update($name,$folder,$tag)
	{
		global $db,$root_path;
		$query = array (
			'catname' 			=> $name,
			'tag'	=> $tag,
			'folder'		=>	$folder,
		);
		$cat=new Category($this->data['pid']);
		mkdir($root_path . 'images/'.$cat->folder . $folder);
		$query_str = "UPDATE categories SET " . $db->sql_build_array('UPDATE',$query) . " WHERE " . $db->sql_in_set("id",array($this->data['id']));
		return $db->sql_query($query_str);
        $this->GetInfo();
	}
	public static function Delete($id)
	{
		global $db;
		return $db->sql_query("DELETE FROM categories WHERE " . $db->sql_in_set("id",array($id)));
	}
	private static function GetInfo1($id)
	{
		global $db;
		$query = "SELECT * FROM categories WHERE " . $db->sql_in_set("id",array($id));
		$result = $db->sql_query($query);
		$row=$db->sql_fetchrow($result);
		return $row;
	}
	private function GetItems()
	{
		global $db;
		$query_r=array(
			'SELECT'	=> '*',
			'FROM'		=>array(
				'items'	=>	'i',
			),
			'WHERE'		=> $db->sql_in_set('catid',array($this->data['id'])),
		);
		//echo $db->sql_build_query('SELECT',$query_r);
		$result=$db->sql_query($db->sql_build_query('SELECT',$query_r));
		$rowset=array();
		while($row=$db->sql_fetchrow($result))
		{
            $rowset[$row['tag'].$row['id']]=$row;
			$query_r=array(
				'SELECT'	=> 'filename',
				'FROM'		=> array('item_images'	=> 'ii'),
				'WHERE'		=> $db->sql_in_set('tag',array($row['tag'])) .' AND '. $db->sql_in_set('id',array($row['id'])),
			);
			$result1=$db->sql_query($db->sql_build_query('SELECT',$query_r));
			$j=0;
			while($row1=$db->sql_fetchrow($result1))
				$rowset[$row['tag'].$row['id']]['images'][$j++]=$row1['filename'];
		}
		return $rowset;
	}
	public function GetAllItems($save=true,$page=1)
	{
		$items=$this->items;
		if(count($this->all_items)==0) //neu chua co item trong all_items -> doc
		{
			foreach($this->sub as $sub)
			{
				$cat=new Category($sub['id']);
				$tmp=$cat->GetAllItems(false);
				if(count($tmp)>0)
					foreach($tmp as $key =>$data)
					{
						$items[$key]=$data;
					}
			}
			if($save) $this->all_items=array_values($items); //save items vao all_items
		}
		if($save){
			//tinh so trang
			$this->page_num=count($items)%$this->max_item==0?count($items)/$this->max_item:(int)(count($items)/$this->max_item)+1;
			$tmp=array();
			$item_count=(count($items)-($page-1)*$this->max_item)>$this->max_item?$this->max_item:(count($items)-($page-1)*$this->max_item);
			for($i=0;$i<$item_count;$i++)
				$tmp[$i]=$this->all_items[($page-1)*$this->max_item+$i];
			return $tmp;
		}
		return $items;
	}
	public function Search($keyword)
	{
		
		$keyword=' '.strtolower(utf8_to_ascii($keyword));
		if(count($this->all_items)==0)
			$this->GetAllItems(true);
		$result=array();
		$i=0;
		foreach($this->all_items as $item)
		{
			$data=strtolower(utf8_to_ascii(' '.$item['name'].' '.$item['tag'].'-'.$item['id'].' '.$item['price']));
			if(strpos($data,$keyword)!==false)
				$result[$i++]=$item;
		}
		$this->page_num=$i;
		return $result;
	}
}
?>